package de.openlaboratory.elib.model.library;

import java.io.Serializable;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import de.openlaboratory.elib.controller.AuthorDAO;
import de.openlaboratory.elib.controller.LibraryDAO;

@Entity
@Table(name="PUBLICATIONS")
public class Publication implements Serializable
{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 3723687207554830485L;

	@Id
	@GeneratedValue
	private Long id;
	
	private String title;
	private String subtitle;
	private String author;
	private Boolean et_al;
	
	@ManyToOne
    @JoinColumn(name="LIBRARY_ID")
	private Library library;

	@OneToMany(fetch = FetchType.EAGER)
	@JoinTable(name = "PUBLICATION_AUTHORS",
		joinColumns = { @JoinColumn(name = "PUBLICATION_ID") },
		inverseJoinColumns = { @JoinColumn(name = "AUTHOR_ID") })
	private Set<Author> authors;

	public Publication()
	{
		super();
	}

	public Publication(String title)
	{
		super();
		
		this.title = title;
	}
	
	public Publication(long id)
	{
		super();
		
		this.id = id;
	}

	public Publication(Long id, String title, String subtitle, String authors, Long[] authorIDs, Long libraryId)
	{
		super();
		
		this.id = id;
		this.title = title;
		this.subtitle = subtitle;
		this.author = authors;
		this.library = new LibraryDAO().find(libraryId);
		this.authors = new AuthorDAO().findByIds(authorIDs);
	}

	/**
	 * GETTERS and SETTERS
	 */
	
    public Library getLibrary() {
        return library;
    }
	
	public void setLibrary(Library library) {
		this.library = library;
	}
	
	public String getTitle()
	{
		return title;
	}

	public void setTitle(String title)
	{
		this.title = title;
	}

	public String getAuthor()
	{
		return author;
	}

	public void setAuthor(String author)
	{
		this.author = author;
	}

	public Long getId()
	{
		return id;
	}

	public String getSubtitle()
	{
		return subtitle;
	}

	public void setSubtitle(String subtitle)
	{
		this.subtitle = subtitle;
	}
	
	public Boolean getEtAl() {
		return et_al;
	}

	public void setEtAl(Boolean furtherAuthors) {
		this.et_al = furtherAuthors;
	}

	public Long getLibraryId() {
		if (this.library != null)
			return this.library.getId();
		
		return (long) -1;
	}

	public void setLibraryId(Long library_id) {
		this.library = new LibraryDAO().find(library_id);
	}

	public Set<Author> getAuthors() {
		return authors;
	}

	public void setAuthors(Set<Author> authors) {
		this.authors = authors;
	}
}
